package com.archgeek.java.biz;


import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class GoogleAuthenticator {

    public static void main(String[] args){
        try {
            // String data = "+Cxw1xrUaTT8MCmwVEe7mR3CrcJhYLbp";
            String data = "+Cxw1xrUaTT8MCmwVEe7mR3CrcJhYLbp";
            String KEY = "F#Q#L#!@"; //yg\/IKHuL+6\/y+RFobaYNNR3CrcJhYLbp
            String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";

            // 创建一个DESKeySpec 对象
            DESKeySpec desKey = new DESKeySpec(KEY.getBytes());

            //创建一个密匙工厂，然后用它把DESKeySpec转换成
            SecretKey secretKey = SecretKeyFactory.getInstance("DES").generateSecret(desKey);
            System.out.println("secretKey:"+secretKey);

            // 实例化Cipher对象，它用于完成实际的解密操作
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

            // 用密钥初始化Cipher对象，设置为解密模式
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            // 执行解密操作
            String result = new String(cipher.doFinal(Base64.decodeBase64(data)));
            System.out.println("input="+data);
            System.out.println("output="+result);
            //return result;
            //System.out.println(new String(cipher.doFinal(Base64.decodeBase64(data))));
            //return new String(cipher.doFinal(Base64.decodeBase64(data)));
        } catch (Exception e) {
            System.out.println(e);
        }
    }

}
